<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-button type="primary" :icon="Plus" @click="handleDialogValue"
          >添加申请</el-button
        >
      </el-col>
    </el-row>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column
        :width="item.width"
        :prop="item.prop"
        :label="item.label"
        v-for="(item, index) in options"
        :key="index"
      >
        <template v-slot="{ row }" v-if="item.prop === 'state'">
          <el-tag
            type="success"
            class="mx-1"
            effect="dark"
            round
            v-if="row.state === '0'"
          >
            已同意
          </el-tag>
          <el-tag
            type="warning"
            class="mx-1"
            effect="dark"
            round
            v-if="row.state === '2'"
          >
            待批阅
          </el-tag>
          <el-tag
            type="danger"
            class="mx-1"
            effect="dark"
            round
            v-if="row.state === '1'"
          >
            已拒绝
          </el-tag>
          <el-tag
            type="danger"
            class="mx-1"
            effect="dark"
            round
            v-if="row.state === '3'"
          >
            已取消
          </el-tag>
        </template>
        <template #default="{ row }" v-else-if="item.prop === 'action'">
          <template v-if="row.state === '2'">
            <el-button type="primary" size="small" @click="deleteApply(row)"
              >取消</el-button
            >
          </template>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <Dialog
    v-model="dialogVisible"
    :dialogTitle="dialogTitle"
    v-if="dialogVisible"
    @initUserList="initGetUsers"
    :dialogTableValue="dialogTableValue"
  />
</template>

<script setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { getApply, editApply } from '@/api/apply.js'
import { getDoctor } from '@/api/doctor.js'
import { options } from './options'
import Dialog from './components/dialog.vue'

const dialogTitle = ref('')
const dialogVisible = ref(false)
const dialogTableValue = ref({})

const total = ref(0)
const tableData = ref([])
const initGetUsers = async () => {
  const res1 = await getDoctor()
  const res = await getApply()
  console.log(res1)
  const a = localStorage.getItem('Doctoraccount') + ''
  let dname
  for (let i = 0; i < res.data.total; i++) {
    if (res1.data.datas[i].account === a) {
      dname = res1.data.datas[i].dname
      console.log(1)
      break
    }
  }
  console.log(dname)
  tableData.value = []
  for (let i = 0; i < res.data.total; i++) {
    if (res.data.datas[i].dname === dname) {
      tableData.value.push(res.data.datas[i])
      console.log(dname)
    }
  }
  total.value = res.data.total
}
initGetUsers()

const handleDialogValue = () => {
  dialogTitle.value = '添加申请'
  dialogVisible.value = true
  dialogTableValue.value = {}
}

const deleteApply = async (row) => {
  const label = { aid: row.aid, state: '3' }
  await editApply(label)
  initGetUsers()
}
</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}
</style>